package com.xindaoapp.happypet.ui;

import java.util.List;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.nostra13.universalimageloader.core.ImageLoader;
import com.xindaoapp.happypet.R;
import com.xindaoapp.happypet.activity.MainActivity;
import com.xindaoapp.happypet.application.HappyPetApplication;
import com.xindaoapp.happypet.bean.PostListEntity.ForumInfo;
import com.xindaoapp.happypet.bean.PostListEntity.ForumInfo.Banzhuarray;
import com.xindaoapp.happypet.fragments.UserInfoFragment;
import com.xindaoapp.happypet.utils.CommonParameter.UserState;
import com.xindaoapp.happypet.utils.CommonUtil;
import com.xindaoapp.happypet.utils.Constants;
import com.xindaoapp.happypet.utils.IRequest;
import com.xindaoapp.happypet.utils.ProgressHUD;
import com.xindaoapp.happypet.view.CircleImageView;
import com.xindaoapp.happypet.weight.AutoMeasureGridView;

/**
 * 圈子详情
 * 
 * @author Yangqing
 * 
 */
public class ClubDetailActivity extends BaseActivity implements OnClickListener {

    private ImageView vIcon, vAddDelete;
    private TextView vName, vDescrib, vTieziNum, vChengyuanNum, vRule;
    // private MyGridView vBanzhu, vDaren;
    private View noData;

    private AutoMeasureGridView vBanzhu, vDaren;

    private final String filter = "lastpost";
    private String name;
    private String fId;

    private ForumInfo mForumInfo;


    private MyAdapter1 banzhuAdapter, darenAdapter;


    private List<Banzhuarray> banzhuList;// 列表
    private List<Banzhuarray> darenList;// 达人列表

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);



    }



    @Override
    protected int getContentViewResId() {
        // TODO Auto-generated method stub
        return R.layout.activity_club_detail;
    }


    @Override
    protected void initViews() {
        // TODO Auto-generated method stub
        noData = this.findViewById(R.id.no_data);

        vIcon = (ImageView) this.findViewById(R.id.iv_icon);
        vAddDelete = (ImageView) this.findViewById(R.id.iv_add_delete);

        vName = (TextView) this.findViewById(R.id.tv_name);
        vDescrib = (TextView) this.findViewById(R.id.tv_describ);
        vTieziNum = (TextView) this.findViewById(R.id.tv_tiezi_num);
        vChengyuanNum = (TextView) this.findViewById(R.id.tv_chengyuan_num);
        vRule = (TextView) this.findViewById(R.id.tv_rule);


        vBanzhu = (AutoMeasureGridView) this.findViewById(R.id.banzhu);
        vDaren = (AutoMeasureGridView) this.findViewById(R.id.daren);

        super.initViews();
    }


    @Override
    protected void initEvents() {
        // TODO Auto-generated method stub
        vAddDelete.setOnClickListener(this);
        super.initEvents();
    }


    @Override
    protected void onLoadDatas() {
        // TODO Auto-generated method stub

        super.onLoadDatas();

        getData();


    }


    private void getData() {


        mForumInfo = (ForumInfo) getIntent().getSerializableExtra("forumInfo");
        name = getIntent().getStringExtra("name");


        onDataArrived(true);

        if (mForumInfo != null) {
            setData();
        }


        // fId = getIntent().getStringExtra("fid");
        //
        // if (!CommonUtil.checkNetState(ClubDetailActivity.this)) {
        // Toast.makeText(getApplicationContext(), "当前网络异常,请检查网络!", 1).show();
        // } else {
        // getMoccaApi().getClubDetail(fId, new IRequest<PostListEntity2>() {
        //
        // @Override
        // public void request(PostListEntity2 t) {
        //
        // if (t != null) {
        // onDataArrived(true);
        // setData(t);
        // } else {
        // onDataArrived(false);
        // }
        //
        //
        // }
        // });
        // }



    }



    private void setData() {



        if (mForumInfo.darenarray != null && mForumInfo.darenarray.size() > 0) {

            darenList = mForumInfo.darenarray;

            darenAdapter = new MyAdapter1(darenList);
            vDaren.setAdapter(darenAdapter);

        }
        if (mForumInfo.banzhuarray != null && mForumInfo.banzhuarray.size() > 0) {

            banzhuList = mForumInfo.banzhuarray;

            banzhuAdapter = new MyAdapter1(banzhuList);
            vBanzhu.setAdapter(banzhuAdapter);
        }



        vName.setText(mForumInfo.name);
        vDescrib.setText(mForumInfo.description);
        vTieziNum.setText(mForumInfo.posts);
        vChengyuanNum.setText(mForumInfo.membernum);
        vRule.setText(mForumInfo.apprules);


        ImageLoader.getInstance().displayImage(mForumInfo.ico, vIcon);

    }



    @Override
    protected OnClickListener getLeftViewClickListener() {
        return new OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        };
    }



    @Override
    protected int getTopBarLeftImageViewRes() {
        return R.drawable.top_bar_back;
    }



    @Override
    protected String getTopBarTitleStrRes() {
        // TODO Auto-generated method stub
        return "圈详情";
    }

    /** 我加入的圈子 */
    private class MyAdapter1 extends BaseAdapter {
        private final List<Banzhuarray> lst;

        public MyAdapter1(List<Banzhuarray> groups) {
            super();
            this.lst = groups;
        }

        @Override
        public int getCount() {
            return lst.size();
        }

        public List<Banzhuarray> getItems() {
            if (null != lst) {
                return lst;
            }
            return null;
        }

        @Override
        public Object getItem(int position) {
            return position;
        }

        @Override
        public long getItemId(int position) {
            return position;
        }

        @Override
        public View getView(final int position, View convertView, ViewGroup parent) {
            ViewHolder holder;
            if (convertView == null) {
                holder = new ViewHolder();
                convertView = View.inflate(ClubDetailActivity.this, R.layout.item_clubdetail, null);
                holder.icon = (CircleImageView) convertView.findViewById(R.id.icon);
                holder.name = (TextView) convertView.findViewById(R.id.name);
                convertView.setTag(holder);
            } else {
                holder = (ViewHolder) convertView.getTag();
            }

            holder.name.setText(lst.get(position).username);
            ImageLoader.getInstance().displayImage(lst.get(position).userface, holder.icon);

            convertView.setOnClickListener(new OnClickListener() {

                @Override
                public void onClick(View arg0) {
                    // TODO Auto-generated method stub



                    Intent intent = new Intent(ClubDetailActivity.this, MainActivity.class);
                    intent.putExtra(Constants.KEY_SKIP_CLASS_NAME,
                            UserInfoFragment.class.getCanonicalName());
                    Bundle bundle = new Bundle();

                    bundle.putString(Constants.KEY_UID, lst.get(position).uid);
                    bundle.putString(Constants.KEY_USERNAME, lst.get(position).username);

                    bundle.putString(Constants.KEY_USERFACE, lst.get(position).userface);
                    intent.putExtra(Constants.KEY_BUNDLE, bundle);
                    ClubDetailActivity.this.startActivity(intent);


                }
            });

            return convertView;
        }

        class ViewHolder {
            ImageView icon;
            TextView name;
        }
    }

    @Override
    public void onClick(View arg0) {
        // TODO Auto-generated method stub
        switch (arg0.getId()) {



            case R.id.iv_add_delete:// 取消加入圈子
                if (Constants.ISMYGROUP) {
                    if (UserState.isLogged(ClubDetailActivity.this)) {
                        if (UserState.isLogged(ClubDetailActivity.this)) {
                            final ProgressHUD ProgressDialog =
                                    ProgressHUD.show(ClubDetailActivity.this, "正在加载...", true,
                                            true, null);
                            HappyPetApplication
                                    .get()
                                    .getMoccaApi()
                                    .outGroup(mForumInfo.fid, ClubDetailActivity.this,
                                            new IRequest<Boolean>() {
                                                @Override
                                                public void request(Boolean t) {
                                                    if (null != ProgressDialog
                                                            && ProgressDialog.isShowing()) {
                                                        ProgressDialog.dismiss();
                                                    }
                                                    if (t) {
                                                        Constants.ISMYGROUP = false;
                                                        vAddDelete
                                                                .setBackgroundResource(R.drawable.post_add);
                                                    } else {
                                                        vAddDelete
                                                                .setBackgroundResource(R.drawable.post_delete);
                                                    }
                                                    sendBroadcast(new Intent(
                                                            Constants.JOIN_OUT_GROUP));
                                                }
                                            });
                        } else {
                            Toast.makeText(getApplicationContext(), R.string.pLease_check_network,
                                    Toast.LENGTH_SHORT).show();
                        }
                    }
                } else if (!Constants.ISMYGROUP) {
                    if (CommonUtil.checkNetState(ClubDetailActivity.this)) {
                        final ProgressHUD ProgressDialog =
                                ProgressHUD.show(ClubDetailActivity.this, "正在加载...", true, true,
                                        null);
                        HappyPetApplication
                                .get()
                                .getMoccaApi()
                                .joinGroup(mForumInfo.fid, ClubDetailActivity.this,
                                        new IRequest<Boolean>() {
                                            @Override
                                            public void request(Boolean t) {
                                                if (null != ProgressDialog
                                                        && ProgressDialog.isShowing()) {
                                                    ProgressDialog.dismiss();
                                                }
                                                if (t) {
                                                    Constants.ISMYGROUP = true;
                                                    vAddDelete
                                                            .setBackgroundResource(R.drawable.post_delete);
                                                } else {
                                                    vAddDelete
                                                            .setBackgroundResource(R.drawable.post_add);
                                                }
                                                sendBroadcast(new Intent(Constants.JOIN_OUT_GROUP));
                                            }
                                        });
                    } else {
                        Toast.makeText(getApplicationContext(), R.string.pLease_check_network,
                                Toast.LENGTH_SHORT).show();
                    }
                }

                break;
            default:
                break;
        }

    }


}
